home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / infcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  26.9 KB  |  1,055 lines

  1. /* Memory-access and commands for inferior process, for GDB.
  2.    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GDB General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute GDB,
  11. but only under the conditions described in the GDB General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with GDB so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17. In other words, go ahead and share GDB, but don't try to stop
  18. anyone else from sharing it farther.  Help stamp out software hoarding!
  19. */
  20.  
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "param.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26. #include "environ.h"
  27. #include "value.h"
  28.  
  29. #include <stdio.h>
  30. #include <signal.h>
  31.  
  32. #ifndef atarist
  33. #include <sys/param.h>
  34. #endif
  35.  
  36. extern char *sys_siglist[];
  37.  
  38. #define ERROR_NO_INFERIOR \
  39.    if (inferior_pid == 0) error ("The program is not being run.");
  40.  
  41. /* String containing arguments to give to the program,
  42.    with a space added at the front.  Just a space means no args.  */
  43.  
  44. static char *inferior_args = (char *)NULL;
  45.  
  46. /* File name for default use for standard in/out in the inferior.  */
  47.  
  48. char *inferior_io_terminal;
  49.  
  50. /* Pid of our debugged inferior, or 0 if no inferior now.  */
  51.  
  52. int inferior_pid;
  53.  
  54. /* Last signal that the inferior received (why it stopped).  */
  55.  
  56. int stop_signal;
  57.  
  58. /* Address at which inferior stopped.  */
  59.  
  60. CORE_ADDR stop_pc;
  61.  
  62. /* Stack frame when program stopped.  */
  63.  
  64. FRAME stop_frame;
  65.  
  66. /* Number of breakpoint it stopped at, or 0 if none.  */
  67.  
  68. int stop_breakpoint;
  69.  
  70. /* Nonzero if stopped due to a step command.  */
  71.  
  72. int stop_step;
  73.  
  74. /* Nonzero if stopped due to completion of a stack dummy routine.  */
  75.  
  76. int stop_stack_dummy;
  77.  
  78. /* Range to single step within.
  79.    If this is nonzero, respond to a single-step signal
  80.    by continuing to step if the pc is in this range.  */
  81.  
  82. CORE_ADDR step_range_start; /* Inclusive */
  83. CORE_ADDR step_range_end; /* Exclusive */
  84.  
  85. /* Stack frame address as of when stepping command was issued.
  86.    This is how we know when we step into a subroutine call,
  87.    and how to set the frame for the breakpoint used to step out.  */
  88.  
  89. CORE_ADDR step_frame;
  90.  
  91. /* 1 means step over all subroutine calls.
  92.    -1 means step over calls to undebuggable functions.  */
  93.  
  94. int step_over_calls;
  95.  
  96. /* If stepping, nonzero means step count is > 1
  97.    so don't print frame next time inferior stops
  98.    if it stops due to stepping.  */
  99.  
  100. int step_multi;
  101.  
  102. /* Environment to use for running inferior,
  103.    in format described in environ.h.  */
  104.  
  105. struct environ *inferior_environ;
  106.  
  107. CORE_ADDR read_pc ();
  108. struct command_line *get_breakpoint_commands ();
  109.  
  110.  
  111. int
  112. have_inferior_p ()
  113. {
  114.   return inferior_pid != 0;
  115. }
  116.  
  117. static void 
  118. set_args_command (args)
  119.      char *args;
  120. {
  121. #ifdef atarist
  122.   if(have_inferior_p())
  123.       error("Cannot set-args after the inferior program has been started\
  124.  on the atariST.\n To set args to be passed to an inferior process either\
  125. \n\tissue the set-args command before the initial run command\
  126. \n\tor specify the args for the inferior process in the initial run command.");
  127. #endif
  128.   
  129.   if(inferior_args)
  130.       free (inferior_args);
  131.   if (!args) args = "";
  132.   inferior_args = concat (" ", args, "");
  133. }
  134.  
  135. void
  136. tty_command (file)
  137.      char *file;
  138. {
  139.   if (file == 0)
  140.     error_no_arg ("terminal name for running target process");
  141.  
  142.   inferior_io_terminal = savestring (file, strlen (file));
  143. }
  144.  
  145. static void
  146. run_command (args, from_tty)
  147.      char *args;
  148.      int from_tty;
  149. {
  150.   extern char **environ;
  151.   register int i;
  152.   char *exec_file;
  153.   char *allargs;
  154.  
  155.   extern int sys_nerr;
  156.   extern char *sys_errlist[];
  157.   extern int errno;
  158.  
  159.   dont_repeat ();
  160.  
  161.   if (inferior_pid)
  162.     {
  163. #ifndef atarist
  164.       if (query ("The program being debugged has been started already.\n\
  165. Start it from the beginning? "))
  166.     kill_inferior ();
  167.       else
  168.     error ("Program already started.");
  169. #else
  170.       error("Program already started. Cannot restart program on the atariST.");
  171. #endif      
  172.     }
  173.  
  174. #ifndef atarist
  175.   if (remote_debugging)
  176.     {
  177.       free (allargs);
  178.       if (from_tty)
  179.     {
  180.      printf_filtered ("Starting program: %s%s\n",
  181.           exec_file, inferior_args);
  182.       fflush (stdout);
  183.     }
  184.     }
  185.   else
  186. #endif
  187.     {
  188.       if (args)
  189.     set_args_command (args);
  190.  
  191.       exec_file = (char *) get_exec_file (1);
  192.       if (from_tty)
  193.     {
  194.      printf_filtered ("Starting program: %s%s\n",
  195.           exec_file, inferior_args);
  196.       fflush (stdout);
  197.     }
  198.  
  199.       allargs = concat ("exec ", exec_file, inferior_args);
  200. #ifndef atarist
  201.       inferior_pid = create_inferior (allargs, environ_vector (inferior_environ));
  202. #else
  203.       i = create_inferior (allargs, environ_vector (inferior_environ));
  204.       if (i == -9999)
  205.         error ("\
  206. Program already started or run once. Cannot restart program on the atariST.\n\
  207. Exit GDB and start again if you want to do this.");
  208.  
  209.       inferior_pid = i;
  210. #endif
  211.     }
  212.  
  213.   clear_proceed_status ();
  214.   start_inferior ();
  215. }
  216.  
  217. void
  218. cont_command (proc_count_exp, from_tty)
  219.      char *proc_count_exp;
  220.      int from_tty;
  221. {
  222.   ERROR_NO_INFERIOR;
  223.  
  224.  
  225.   /* If have argument, set proceed count of breakpoint we stopped at.  */
  226.  
  227.   if (stop_breakpoint && proc_count_exp)
  228.     {
  229.       set_ignore_count (stop_breakpoint,
  230.             parse_and_eval_address (proc_count_exp) - 1,
  231.             from_tty);
  232.       if (from_tty)
  233. printf_filtered ("  ");
  234.     }
  235.  
  236.   if (from_tty)
  237.    printf_filtered ("Continuing.\n");
  238.  
  239.   clear_proceed_status ();
  240.  
  241.   proceed (-1, -1, 0);
  242. }
  243.  
  244. /* Step until outside of current statement.  */
  245. static void step_1 ();
  246.  
  247. static void
  248. step_command (count_string)
  249. {
  250.   step_1 (0, 0, count_string);
  251. }
  252.  
  253. /* Likewise, but skip over subroutine calls as if single instructions.  */
  254.  
  255. static void
  256. next_command (count_string)
  257. {
  258.   step_1 (1, 0, count_string);
  259. }
  260.  
  261. /* Likewise, but step only one instruction.  */
  262.  
  263. static void
  264. stepi_command (count_string)
  265. {
  266.   step_1 (0, 1, count_string);
  267. }
  268.  
  269. static void
  270. nexti_command (count_string)
  271. {
  272.   step_1 (1, 1, count_string);
  273. }
  274.  
  275. static void
  276. step_1 (skip_subroutines, single_inst, count_string)
  277.      int skip_subroutines;
  278.      int single_inst;
  279.      char *count_string;
  280. {
  281.   register int count = 1;
  282.  
  283.   ERROR_NO_INFERIOR;
  284.   count = count_string ? parse_and_eval_address (count_string) : 1;
  285.  
  286.   for (; count > 0; count--)
  287.     {
  288.       clear_proceed_status ();
  289.  
  290.       step_frame = get_current_frame ();
  291.  
  292.       if (!step_frame)            /* Avoid coredump here.  Why tho? */
  293.     error ("No current frame");
  294.  
  295.       if (! single_inst)
  296.     {
  297.       find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
  298.       if (step_range_end == 0)
  299.         {
  300.           int misc;
  301.  
  302.           misc = find_pc_misc_function (stop_pc);
  303.           terminal_ours ();
  304.          printf_filtered ("Current function has no line number information.\n");
  305.           fflush (stdout);
  306.  
  307.           /* No info or after _etext ("Can't happen") */
  308.           if (misc == -1 || misc == misc_function_count - 1)
  309.         error ("No data available on pc function.");
  310.  
  311.          printf_filtered ("Single stepping until function exit.\n");
  312.           fflush (stdout);
  313.  
  314.           step_range_start = misc_function_vector[misc].address;
  315.           step_range_end = misc_function_vector[misc + 1].address;
  316.         }
  317.     }
  318.       else
  319.     {
  320.       /* Say we are stepping, but stop after one insn whatever it does.
  321.          Don't step through subroutine calls even to undebuggable functions.  */
  322.       step_range_start = step_range_end = 1;
  323.       if (!skip_subroutines)
  324.         step_over_calls = 0;
  325.     }
  326.  
  327.       if (skip_subroutines)
  328.     step_over_calls = 1;
  329.  
  330.       step_multi = (count > 1);
  331.       proceed (-1, -1, 1);
  332.       if (! stop_step)
  333.     break;
  334.     }
  335. }
  336.  
  337. /* Continue program at specified address.  */
  338.  
  339. static void
  340. jump_command (arg, from_tty)
  341.      char *arg;
  342.      int from_tty;
  343. {
  344.   register CORE_ADDR addr;
  345.   struct symtab_and_line sal;
  346.  
  347.   ERROR_NO_INFERIOR;
  348.  
  349.   if (!arg)
  350.     error_no_arg ("starting address");
  351.  
  352.   sal = decode_line_spec (arg, 1);
  353.  
  354.   if (sal.symtab == 0 && sal.pc == 0)
  355.     error ("No source file has been specified.");
  356.  
  357.   if (sal.pc == 0)
  358.     sal.pc = find_line_pc (sal.symtab, sal.line);
  359.  
  360.   {
  361.     struct symbol *fn = get_frame_function (get_current_frame ());
  362.     struct symbol *sfn = find_pc_function (sal.pc);
  363.     if (fn != 0 && sfn != fn
  364.     && ! query ("Line %d is not in `%s'.  Jump anyway? ",
  365.             sal.line, SYMBOL_NAME (fn)))
  366.       error ("Not confirmed.");
  367.   }
  368.  
  369.   if (sal.pc == 0)
  370.     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
  371.  
  372.   addr = sal.pc;
  373.  
  374.  
  375.   if (from_tty)
  376.    printf_filtered ("Continuing at 0x%x.\n", addr);
  377.  
  378.   clear_proceed_status ();
  379.   proceed (addr, 0, 0);
  380. }
  381.  
  382. /* Continue program giving it specified signal.  */
  383.  
  384. static void
  385. signal_command (signum_exp, from_tty)
  386.      char *signum_exp;
  387.      int from_tty;
  388. {
  389.   register int signum;
  390.  
  391.   dont_repeat ();        /* Too dangerous.  */
  392.   ERROR_NO_INFERIOR;
  393.  
  394.   if (!signum_exp)
  395.     error_no_arg ("signal number");
  396.  
  397.   signum = parse_and_eval_address (signum_exp);
  398.  
  399.  
  400.   if (from_tty)
  401.    printf_filtered ("Continuing with signal %d.\n", signum);
  402.  
  403.   clear_proceed_status ();
  404.   proceed (stop_pc, signum, 0);
  405. }
  406.  
  407. /* Execute a "stack dummy", a piece of code stored in the stack
  408.    by the debugger to be executed in the inferior.
  409.  
  410.    To call: first, do PUSH_DUMMY_FRAME.
  411.    Then push the contents of the dummy.  It should end with a breakpoint insn.
  412.    Then call here, passing address at which to start the dummy.
  413.  
  414.    The contents of all registers are saved before the dummy frame is popped
  415.    and copied into the buffer BUFFER.
  416.  
  417.    The dummy's frame is automatically popped whenever that break is hit.
  418.    If that is the first time the program stops, run_stack_dummy
  419.    returns to its caller with that frame already gone.
  420.    Otherwise, the caller never gets returned to.  */
  421.  
  422. /* 4 => return instead of letting the stack dummy run.  */
  423.  
  424. static int stack_dummy_testing = 0;
  425.  
  426. void
  427. run_stack_dummy (addr, buffer)
  428.      CORE_ADDR addr;
  429.      REGISTER_TYPE *buffer;
  430. {
  431.   /* Now proceed, having reached the desired place.  */
  432.  
  433.   clear_proceed_status ();
  434.   if (stack_dummy_testing & 4)
  435.     {
  436.       POP_FRAME;
  437.       return;
  438.     }
  439.   proceed_to_finish = 1;    /* We want stop_registers, please... */
  440.   proceed (addr, 0, 0);
  441.  
  442.   if (!stop_stack_dummy)
  443.     error ("\
  444. The program being debugged stopped while in a function called from GDB.\n\
  445. The expression which contained the function call has been discarded.(%d)",
  446.        stop_stack_dummy);
  447.  
  448.   /* On return, the stack dummy has been popped already.  */
  449.  
  450.   bcopy (stop_registers, buffer, sizeof stop_registers);
  451. }
  452. /* ARGSUSED */
  453. void
  454. until_next_command (from_tty)
  455.      int from_tty;
  456. {
  457.   struct frame_info fi;
  458.   CORE_ADDR pc;
  459.   struct symbol *func;
  460.   struct symtab_and_line sal;
  461.  
  462.   clear_proceed_status ();
  463.  
  464.   fi = get_frame_info(get_current_frame ());
  465.  
  466.   /* Step until either exited from this function or greater
  467.      than the current line (if in symbolic section) or pc (if
  468.      not). */
  469.  
  470.   pc = read_pc ();
  471.   func = find_pc_function (pc);
  472.   
  473.   if (!func)
  474.     {
  475.       int misc_func = find_pc_misc_function (pc);
  476.       
  477.       if (misc_func != -1)
  478.     error ("Execution is not within a known function.");
  479.       
  480.       step_range_start = misc_function_vector[misc_func].address;
  481.       step_range_end = pc;
  482.     }
  483.   else
  484.     {
  485.       sal = find_pc_line (pc, 0);
  486.       
  487.       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
  488.       step_range_end = sal.end;
  489.     }
  490.   
  491.   step_over_calls = 1;
  492.   step_frame = fi.frame;
  493.   
  494.   step_multi = 0;        /* Only one call to proceed */
  495.   
  496.   proceed ((CORE_ADDR) -1, -1, 1);
  497. }
  498.  
  499. void 
  500. until_command (arg, from_tty)
  501.      char *arg;
  502.      int from_tty;
  503. {
  504.   if (!inferior_pid)
  505.     error ("The program is not running.");
  506.   if (arg)
  507. /*    until_break_command (arg, from_tty); */
  508.       error("Sorry until <break> command is a no-op on the atariST for now.\n\
  509.  Only the until next command form of until (without args) is supported.");
  510.   else
  511.     until_next_command (from_tty);
  512. }
  513.  
  514. /* "finish": Set a temporary breakpoint at the place
  515.    the selected frame will return to, then continue.  */
  516.  
  517. static void
  518. finish_command (arg, from_tty)
  519.      char *arg;
  520.      int from_tty;
  521. {
  522.   struct symtab_and_line sal;
  523.   register FRAME frame;
  524.   struct frame_info fi;
  525.  
  526.   register struct symbol *function;
  527.  
  528.   if (!have_inferior_p ())
  529.     error ("The program is not being run.");
  530.   if (arg)
  531.     error ("The \"finish\" command does not take any arguments.");
  532.  
  533.   frame = get_prev_frame (selected_frame);
  534.   if (frame == 0)
  535.     error ("\"finish\" not meaningful in the outermost frame.");
  536.  
  537.   clear_proceed_status ();
  538.  
  539.   fi = get_frame_info (frame);
  540.   sal = find_pc_line (fi.pc, 0);
  541.   sal.pc = fi.pc;
  542.   set_momentary_breakpoint (sal, frame);
  543.  
  544.   /* Find the function we will return from.  */
  545.  
  546.   fi = get_frame_info (fi.next_frame);
  547.   function = find_pc_function (fi.pc);
  548.  
  549.   if (from_tty)
  550.     {
  551.      printf_filtered ("Run till exit from ");
  552.       print_selected_frame ();
  553.     }
  554.  
  555.   proceed_to_finish = 1;    /* We want stop_registers, please... */
  556.   proceed (-1, -1, 0);
  557.  
  558.   if (stop_breakpoint == -3 && function != 0)
  559.     {
  560.       struct type *value_type;
  561.       CORE_ADDR funcaddr;
  562.       register value val;
  563.  
  564.       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
  565.       if (!value_type)
  566.     fatal ("internal: finish_command: function has no target type");
  567.  
  568. #if 0
  569.       if (TYPE_CODE (SYMBOL_TYPE (function)) != TYPE_CODE_VOID)
  570.     value_type = SYMBOL_TYPE (function);
  571.       else
  572.     return;
  573. #else
  574.       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
  575.     return;
  576. #endif
  577.  
  578.       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
  579.  
  580.       val = value_being_returned (value_type, stop_registers,
  581.           using_struct_return (value_of_variable (function),
  582.                    funcaddr,
  583.                    value_type,
  584.                    1));
  585.      printf_filtered ("Value returned is $%d = ", record_latest_value (val));
  586.       value_print (val, stdout, 0);
  587.       fputc_filtered ('\n', stdout);
  588.     }
  589. }
  590.  
  591. static void
  592. program_info ()
  593. {
  594.   if (inferior_pid == 0)
  595.     {
  596.      printf_filtered ("The program being debugged is not being run.\n");
  597.       return;
  598.     }
  599.  
  600.  printf_filtered ("Program being debugged is in process %d, stopped at 0x%x.\n",
  601.       inferior_pid, stop_pc);
  602.   if (stop_step)
  603.    printf_filtered ("It stopped after being stepped.\n");
  604.   else if (stop_breakpoint)
  605.    printf_filtered ("It stopped at breakpoint %d.\n", stop_breakpoint);
  606.   else if (stop_signal)
  607.    printf_filtered ("It stopped with signal %d (%s).\n",
  608.         stop_signal,
  609.         (stop_signal > NSIG)? "unknown": sys_siglist[stop_signal]);
  610.  
  611.  printf_filtered ("\nType \"info stack\" or \"info reg\" for more information.\n");
  612. }
  613.  
  614. static void
  615. environment_info (var)
  616.      char *var;
  617. {
  618.   if (var)
  619.     {
  620.       register char *val = get_in_environ (inferior_environ, var);
  621.       if (val)
  622. printf_filtered ("%s = %s\n", var, val);
  623.       else
  624. printf_filtered ("Environment variable \"%s\" not defined.\n", var);
  625.     }
  626.   else
  627.     {
  628.       register char **vector = environ_vector (inferior_environ);
  629.       while (*vector)
  630. printf_filtered ("%s\n", *vector++);
  631.     }
  632. }
  633.  
  634. static void
  635. set_environment_command (arg)
  636.      char *arg;
  637. {
  638.   register char *p, *val, *var;
  639.   int nullset = 0;
  640.  
  641.   if (arg == 0)
  642.     error_no_arg ("environment variable and value");
  643.  
  644.   /* Find seperation between variable name and value */
  645.   p = (char *) strchr (arg, '=');
  646.   val = (char *) strchr (arg, ' ');
  647.  
  648.   if (p != 0 && val != 0)
  649.     {
  650.       /* We have both a space and an equals.  If the space is before the
  651.      equals and the only thing between the two is more space, use
  652.      the equals */
  653.       if (p > val)
  654.     while (*val == ' ')
  655.       val++;
  656.  
  657.       /* Take the smaller of the two.  If there was space before the
  658.      "=", they will be the same right now. */
  659.       p = arg + min (p - arg, val - arg);
  660.     }
  661.   else if (val != 0 && p == 0)
  662.     p = val;
  663.  
  664.   if (p == arg)
  665.     error_no_arg ("environment variable to set");
  666.  
  667.   if (p == 0 || p[1] == 0)
  668.     {
  669.       nullset = 1;
  670.       if (p == 0)
  671.     p = arg + strlen (arg);    /* So that savestring below will work */
  672.     }
  673.   else
  674.     {
  675.       /* Not setting variable value to null */
  676.       val = p + 1;
  677.       while (*val == ' ' || *val == '\t')
  678.     val++;
  679.     }
  680.  
  681.   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
  682.  
  683.   var = savestring (arg, p - arg);
  684.   if (nullset)
  685.     {
  686.      printf_filtered ("Setting environment variable \"%s\" to null value.\n", var);
  687.       set_in_environ (inferior_environ, var, "");
  688.     }
  689.   else
  690.     set_in_environ (inferior_environ, var, val);
  691.   free (var);
  692. }
  693.  
  694. static void
  695. unset_environment_command (var)
  696.      char *var;
  697. {
  698.   if (var == 0)
  699.     error_no_arg ("environment variable");
  700.  
  701.   unset_in_environ (inferior_environ, var);
  702. }
  703.  
  704. /* Read an integer from debugged memory, given address and number of bytes.  */
  705.  
  706. read_memory_integer (memaddr, len)
  707.      CORE_ADDR memaddr;
  708.      int len;
  709. {
  710.   char cbuf;
  711.   short sbuf;
  712.   int ibuf;
  713.   long lbuf;
  714.  
  715.   if (len == sizeof (char))
  716.     {
  717.       read_memory (memaddr, &cbuf, len);
  718.       return cbuf;
  719.     }
  720.   if (len == sizeof (short))
  721.     {
  722.       read_memory (memaddr, &sbuf, len);
  723.       return sbuf;
  724.     }
  725.   if (len == sizeof (int))
  726.     {
  727.       read_memory (memaddr, &ibuf, len);
  728.       return ibuf;
  729.     }
  730.   if (len == sizeof (lbuf))
  731.     {
  732.       read_memory (memaddr, &lbuf, len);
  733.       return lbuf;
  734.     }
  735.   error ("Cannot handle integers of %d bytes.", len);
  736. }
  737.  
  738. CORE_ADDR
  739. read_pc ()
  740. {
  741.   return (CORE_ADDR) read_register (PC_REGNUM);
  742. }
  743.  
  744. write_pc (val)
  745.      CORE_ADDR val;
  746. {
  747.   write_register (PC_REGNUM, (long) val);
  748.   pc_changed = 0;
  749. }
  750.  
  751. char *reg_names[] = REGISTER_NAMES;
  752.  
  753. static void
  754. registers_info (addr_exp)
  755.      char *addr_exp;
  756. {
  757.   register int i;
  758.   int regnum;
  759.  
  760.   if (addr_exp)
  761.     {
  762.       if (*addr_exp >= '0' && *addr_exp <= '9')
  763.     regnum = atoi (addr_exp);
  764.       else
  765.     {
  766.       register char *p = addr_exp;
  767.       if (p[0] == '$')
  768.         p++;
  769.       for (regnum = 0; regnum < NUM_REGS; regnum++)
  770.         if (!strcmp (p, reg_names[regnum]))
  771.           break;
  772.       if (regnum == NUM_REGS)
  773.         error ("%s: invalid register name.", addr_exp);
  774.     }
  775.     }
  776.   else
  777.    printf_filtered ("Reg\tContents\n\n");
  778.  
  779.   for (i = 0; i < NUM_REGS; i++)
  780.     {
  781.       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  782.       unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  783.       REGISTER_TYPE val;
  784.  
  785.       if (addr_exp != 0 && i != regnum)
  786.     continue;
  787.  
  788.       /* On machines with lots of registers, pause every 16 lines
  789.      so user can read the output.  */
  790.       if (addr_exp == 0 && i > 0 && i % 16 == 0)
  791.     {
  792.      printf_filtered ("--Type Return to print more--");
  793.       fflush (stdout);
  794.       read_line ();
  795.     }
  796.  
  797.       /* Get the data in raw format, then convert also to virtual format.  */
  798.       read_relative_register_raw_bytes (i, raw_buffer);
  799.       REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
  800.  
  801.      printf_filtered ("%s\t", reg_names[i]);
  802.  
  803.       /* If virtual format is floating, print it that way.  */
  804.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
  805.       && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
  806.     val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, stdout, 0);
  807.       /* Else if virtual format is too long forprintf_filtered,
  808.      print in hex a byte at a time.  */
  809.       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
  810.     {
  811.       register int j;
  812.      printf_filtered ("0x");
  813.       for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
  814.        printf_filtered ("%02x", virtual_buffer[j]);
  815.     }
  816.       /* Else print as integer in hex and in decimal.  */
  817.       else
  818.     {
  819.       long val;
  820.  
  821.       bcopy (virtual_buffer, &val, sizeof (long));
  822.       if (val == 0)
  823.        printf_filtered ("0");
  824.       else
  825.        printf_filtered ("0x%08x  %d", val, val);
  826.     }
  827.  
  828.       /* If register has different raw and virtual formats,
  829.      print the raw format in hex now.  */
  830.  
  831.       if (REGISTER_CONVERTIBLE (i))
  832.     {
  833.       register int j;
  834.  
  835.      printf_filtered ("  (raw 0x");
  836.       for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
  837.        printf_filtered ("%02x", raw_buffer[j]);
  838.      printf_filtered (")");
  839.     }
  840.      printf_filtered ("\n");
  841.     }
  842.  
  843.  printf_filtered ("Contents are relative to selected stack frame.\n");
  844. }
  845.  
  846. #ifdef ATTACH_DETACH
  847. /*
  848.  * TODO:
  849.  * Should save/restore the tty state since it might be that the
  850.  * program to be debugged was started on this tty and it wants
  851.  * the tty in some state other than what we want.  If it's running
  852.  * on another terminal or without a terminal, then saving and
  853.  * restoring the tty state is a harmless no-op.
  854.  */
  855.  
  856. /*
  857.  * attach_command --
  858.  * takes a program started up outside of gdb and ``attaches'' to it.
  859.  * This stops it cold in its tracks and allows us to start tracing it.
  860.  * For this to work, we must be able to send the process a
  861.  * signal and we must have the same effective uid as the program.
  862.  */
  863. static void
  864. attach_command (args, from_tty)
  865.      char *args;
  866.      int from_tty;
  867. {
  868.   char *exec_file;
  869.   int pid;
  870.   int remote = 0;
  871.  
  872.   dont_repeat();
  873.  
  874.   if (!args)
  875.     error_no_arg ("process-id to attach");
  876.  
  877.   while (*args == ' ' || *args == '\t') args++;
  878.  
  879.   if (args[0] == '/')
  880.     remote = 1;
  881.   else
  882.     pid = atoi (args);
  883.  
  884.   if (inferior_pid)
  885.     {
  886.       if (query ("A program is being debugged already.  Kill it? "))
  887.     kill_inferior ();
  888.       else
  889.     error ("Inferior not killed.");
  890.     }
  891.  
  892.   exec_file = (char *) get_exec_file (1);
  893.  
  894.   if (from_tty)
  895.     {
  896.       if (remote)
  897. printf_filtered ("Attaching remote machine\n");
  898.       else
  899. printf_filtered ("Attaching program: %s pid %d\n",
  900.         exec_file, pid);
  901.       fflush (stdout);
  902.     }
  903.  
  904.   if (remote)
  905.     {
  906.       remote_open (args, from_tty);
  907.       start_remote ();
  908.     }
  909.   else
  910.     attach_program (pid);
  911. }
  912.  
  913. /*
  914.  * detach_command --
  915.  * takes a program previously attached to and detaches it.
  916.  * The program resumes execution and will no longer stop
  917.  * on signals, etc.  We better not have left any breakpoints
  918.  * in the program or it'll die when it hits one.  For this
  919.  * to work, it may be necessary for the process to have been
  920.  * previously attached.  It *might* work if the program was
  921.  * started via the normal ptrace (PTRACE_TRACEME).
  922.  */
  923.  
  924. static void
  925. detach_command (args, from_tty)
  926.      char *args;
  927.      int from_tty;
  928. {
  929.   int signal = 0;
  930.  
  931.   if (!inferior_pid)
  932.     error ("Not currently tracing a program\n");
  933.   if (from_tty)
  934.     {
  935.       char *exec_file = (char *)get_exec_file (0);
  936.       if (exec_file == 0)
  937.     exec_file = "";
  938.      printf_filtered ("Detaching program: %s pid %d\n",
  939.           exec_file, inferior_pid);
  940.       fflush (stdout);
  941.     }
  942.   if (args)
  943.     signal = atoi (args);
  944.  
  945.   detach (signal);
  946.   inferior_pid = 0;
  947. }
  948. #endif /* ATTACH_DETACH */
  949.  
  950. void
  951. initialize_infcmd ()
  952. {
  953.   add_com ("tty", class_run, tty_command,
  954.        "Set terminal for future runs of program being debugged.");
  955.  
  956.   add_com ("set-args", class_run, set_args_command,
  957.        "Specify arguments to give program being debugged when it is started.\n\
  958. Follow this command with any number of args, to be passed to the program.");
  959.  
  960.   add_info ("environment", environment_info,
  961.         "The environment to give the program, or one variable's value.\n\
  962. With an argument VAR, prints the value of environment variable VAR to\n\
  963. give the program being debugged.  With no arguments, prints the entire\n\
  964. environment to be given to the program.");
  965.  
  966.   add_com ("unset-environment", class_run, unset_environment_command,
  967.        "Cancel environment variable VAR for the program.\n\
  968. This does not affect the program until the next \"run\" command.");
  969.   add_com ("set-environment", class_run, set_environment_command,
  970.        "Set environment variable value to give the program.\n\
  971. Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
  972. VALUES of environment variables are uninterpreted strings.\n\
  973. This does not affect the program until the next \"run\" command.");
  974.  
  975. #ifdef ATTACH_DETACH
  976.  add_com ("attach", class_run, attach_command,
  977.         "Attach to a process that was started up outside of GDB.\n\
  978. To do this, you must have permission to send the process a signal.\n\
  979. And it must have the same effective uid as the debugger.\n\n\
  980. Before using \"attach\", you must use the \"exec-file\" command\n\
  981. to specify the program running in the process,\n\
  982. and the \"symbol-file\" command to load its symbol table.");
  983.   add_com ("detach", class_run, detach_command,
  984.        "Detach the process previously attached.\n\
  985. The process is no longer traced and continues its execution.");
  986. #endif /* ATTACH_DETACH */
  987.  
  988.   add_com ("signal", class_run, signal_command,
  989.        "Continue program giving it signal number SIGNUMBER.");
  990.  
  991.   add_com ("stepi", class_run, stepi_command,
  992.        "Step one instruction exactly.\n\
  993. Argument N means do this N times (or till program stops for another reason).");
  994.   add_com_alias ("si", "stepi", class_alias, 0);
  995.  
  996.   add_com ("nexti", class_run, nexti_command,
  997.        "Step one instruction, but proceed through subroutine calls.\n\
  998. Argument N means do this N times (or till program stops for another reason).");
  999.   add_com_alias ("ni", "nexti", class_alias, 0);
  1000.  
  1001.   add_com ("finish", class_run, finish_command,
  1002.        "Execute until selected stack frame returns.\n\
  1003. Upon return, the value returned is printed and put in the value history.");
  1004.  
  1005.   add_com ("next", class_run, next_command,
  1006.        "Step program, proceeding through subroutine calls.\n\
  1007. Like the \"step\" command as long as subroutine calls do not happen;\n\
  1008. when they do, the call is treated as one instruction.\n\
  1009. Argument N means do this N times (or till program stops for another reason).");
  1010.   add_com_alias ("n", "next", class_run, 1);
  1011.  
  1012.   add_com ("step", class_run, step_command,
  1013.        "Step program until it reaches a different source line.\n\
  1014. Argument N means do this N times (or till program stops for another reason).");
  1015.   add_com_alias ("s", "step", class_run, 1);
  1016.  
  1017.   add_com ("until", class_run, until_command,
  1018.        "Execute until the program reaches a source line greater than the current\n\
  1019. or a specified line or address or function (same args as break command).\n\
  1020. Execution will also stop upon exit from the current stack frame.");
  1021.   add_com_alias ("u", "until", class_run, 1);
  1022.  
  1023.   add_com ("jump", class_run, jump_command,
  1024.        "Continue program being debugged at specified line or address.\n\
  1025. Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
  1026. for an address to start at.");
  1027.  
  1028.   add_com ("cont", class_run, cont_command,
  1029.        "Continue program being debugged, after signal or breakpoint.\n\
  1030. If proceeding from breakpoint, a number N may be used as an argument:\n\
  1031. then the same breakpoint won't break until the Nth time it is reached.");
  1032.   add_com_alias ("c", "cont", class_run, 1);
  1033.   add_com_alias ("continue", "cont", class_run, 1);
  1034.  
  1035.   add_com ("run", class_run, run_command,
  1036.        "Start debugged program.  You may specify arguments to give it.\n\
  1037. Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
  1038. Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
  1039. With no arguments, uses arguments last specified (with \"run\" or \"set-args\".\n\
  1040. To cancel previous arguments and run with no arguments,\n\
  1041. use \"set-args\" without arguments.");
  1042.   add_com_alias ("r", "run", class_run, 1);
  1043.  
  1044.   add_info ("registers", registers_info,
  1045.         "List of registers and their contents, for selected stack frame.\n\
  1046. Register name as argument means describe only that register.");
  1047.  
  1048.   add_info ("program", program_info,
  1049.         "Execution status of the program.");
  1050.  
  1051.   inferior_args = savestring (" ", 1); /* By default, no args.  */
  1052.   inferior_environ = make_environ ();
  1053.   init_environ (inferior_environ);
  1054. }
  1055.